package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Role;
import com.itheima.service.*;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author Tony
 * @create 2021-12-07 16:37
 */
@RestController
@RequestMapping("/roleManage")
public class RoleManageController {

    @Reference
    private RoleService1 roleService;

    @Reference
    private MenuService1 menuService1;

    @Reference
    private PermissionService1 permissionService;

    @GetMapping("/findPage")
    public Result findPage(QueryPageBean queryPageBean) {
        try {
            PageResult pageResult = roleService.findPage(queryPageBean);
            return new Result(true,"查询角色成功",pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询角色失败");
        }
    }

    @GetMapping("/findAllMenu")
    public Result  findAllMenu(){
        try {
            List<Menu> allMenu = menuService1.findAllMenu();
            return new Result(true,"查询菜单信息成功",allMenu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询菜单信息失败");
        }
    }

    @GetMapping("/findAllPermission")
    public Result findAllPermissions(){
        try {
            List<Map<String, Object>> allPermission = permissionService.findAllPermission();
            return new Result(true,"查询权限成功",allPermission);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询权限失败");
        }
    }

    @PostMapping("/add")
    public Result add(@RequestBody Role role,Integer[] menuIds,Integer[] permissionIds){
        try {
            roleService.addRole(role,menuIds,permissionIds);
            return new Result(true,"添加角色成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"添加角色失败");
        }
    }

    @GetMapping("/findRole")
    public Result findRole(Integer roleId){
        try {
            Role role = roleService.findRoleById(roleId);
            return new Result(true, "查询成功",role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询失败");
        }
    }

    @GetMapping("/findMenuByRoleId")
    public Result findMenuByRoleId(Integer roleId){
        try {
            Integer[] menus = menuService1.findMenuByRoleId(roleId);
            return new Result(true,"查询成功",menus);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询失败");

        }
    }

    @GetMapping("/findPermissionByRoleId")
    public Result findPermissionByRoleId(Integer roleId){
        try {
            Integer[] permissions = permissionService.findPermissionIdByRoleId(roleId);
            return new Result(true,"查询成功",permissions);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询失败");
        }
    }

    @DeleteMapping("/deleteRoleById/{roleId}")
    public Result deleteRoleById(@PathVariable Integer roleId){
        try {
            roleService.deleteRoleById(roleId);
            return new Result(true, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除失败");
        }
    }
    @PostMapping("/updateRole")
    public Result updateRole(@RequestBody Role role){
        try {
            roleService.updateRole(role);
            return new Result(true, "修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "修改失败");
        }
    }

    @GetMapping("/updateMenuAndRole")
    public Result updateMenuAndRole(Integer[] menuIds1,Integer roleId){
        try {
            roleService.updateMenuAndRole(menuIds1,roleId);
            return new Result(true, "修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "修改失败");
        }
    }

    @GetMapping("/updatePermissionAndRole")
    public Result updatePermissionAndRole(Integer[] permissionIds1,Integer roleId){
        try {
            roleService.updatePermission(permissionIds1,roleId);
            return new Result(true, "修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "修改失败");
        }
    }
}
